home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / wot-20080519-fx.xpi / chrome / wot.jar / content / popup.js < prev    next >
Text File  |  2008-05-08  |  10KB  |  399 lines

  1. /*
  2.     popup.js
  3.  
  4.     Copyright ⌐ 2006, 2007  Against Intuition, Inc. <info@mywot.com>
  5. */
  6.  
  7. const WOT_POPUP_LAYER =
  8.     "<div id=\"wot-logo\"></div>" +
  9.     "<div id=\"wot-ratings\">" +
  10.         "<div id=\"wot-r0-stack\" class=\"wot-stack\">" +
  11.             "<div id=\"wot-r0-header\" class=\"wot-header\">WOT_POPUP_TEXT_0</div>" +
  12.             "<div id=\"wot-r0-rep\" class=\"wot-rep\"></div>" +
  13.             "<div id=\"wot-r0-cnf\" class=\"wot-cnf\"></div>" +
  14.         "</div>" +
  15.         "<div id=\"wot-r1-stack\" class=\"wot-stack\">" +
  16.             "<div id=\"wot-r1-header\" class=\"wot-header\">WOT_POPUP_TEXT_1</div>" +
  17.             "<div id=\"wot-r1-rep\" class=\"wot-rep\"></div>" +
  18.             "<div id=\"wot-r1-cnf\" class=\"wot-cnf\"></div>" +
  19.         "</div>" +
  20.         "<div id=\"wot-r2-stack\" class=\"wot-stack\">" +
  21.             "<div id=\"wot-r2-header\" class=\"wot-header\">WOT_POPUP_TEXT_2</div>" +
  22.             "<div id=\"wot-r2-rep\" class=\"wot-rep\"></div>" +
  23.             "<div id=\"wot-r2-cnf\" class=\"wot-cnf\"></div>" +
  24.         "</div>" +
  25.         "<div id=\"wot-r4-stack\" class=\"wot-stack\">" +
  26.             "<div id=\"wot-r4-header\" class=\"wot-header\">WOT_POPUP_TEXT_4</div>" +
  27.             "<div id=\"wot-r4-rep\" class=\"wot-rep\"></div>" +
  28.             "<div id=\"wot-r4-cnf\" class=\"wot-cnf\"></div>" +
  29.         "</div>" +
  30.     "</div>";
  31.  
  32. const WOT_POPUP_STYLE =
  33.     "@import \"chrome://wot/skin/include/popup.css\";";
  34.  
  35. var wot_popup =
  36. {
  37.     offset:            15,
  38.     height:            235,
  39.     width:            137,
  40.     ratingheight:    52,
  41.     areaheight:        214,
  42.     barsize:        20,
  43.     offsetheight:    0,
  44.     id:                "wot-popup-layer-" + Date.now().toString(),
  45.  
  46.     init: function()
  47.     {
  48.         try {
  49.             window.addEventListener("load", function(e) {
  50.                     wot_popup.load();
  51.                 }, false);
  52.             window.addEventListener("unload", function(e) {
  53.                     wot_popup.unload();
  54.                 }, false);
  55.         } catch (e) {
  56.             dump("wot_popup.init: failed with " + e + "\n");
  57.         }
  58.     },
  59.  
  60.     load: function()
  61.     {
  62.         try {
  63.             if (this.browser) {
  64.                 return;
  65.             }
  66.  
  67.             this.browser = document.getElementById("appcontent");
  68.  
  69.             if (this.browser) {
  70.                 this.browser.addEventListener("mouseover",
  71.                     wot_popup.onmouseover, false);
  72.             }
  73.         } catch (e) {
  74.             dump("wot_popup.load: failed with " + e + "\n");
  75.         }
  76.     },
  77.  
  78.     unload: function()
  79.     {
  80.         try {
  81.             if (this.browser) {
  82.                 this.browser.removeEventListener("mouseover",
  83.                         wot_popup.onmouseover, false);
  84.                 this.browser = null;
  85.             }
  86.         } catch (e) {
  87.             dump("wot_popup.unload: failed with " + e + "\n");
  88.         }
  89.     },
  90.  
  91.     add_popup: function(content)
  92.     {
  93.         try {
  94.             if (!wot_prefs.show_search_popup) {
  95.                 return false;
  96.             }
  97.  
  98.             if (!this.layer) {
  99.                 this.layer = WOT_POPUP_LAYER;
  100.                 this.layer = this.layer.replace(/WOT_POPUP_TEXT_0/g,
  101.                     wot_util.getstring("rating_0") + ":");
  102.                 this.layer = this.layer.replace(/WOT_POPUP_TEXT_1/g,
  103.                     wot_util.getstring("rating_1") + ":");
  104.                 this.layer = this.layer.replace(/WOT_POPUP_TEXT_2/g,
  105.                     wot_util.getstring("rating_2") + ":");
  106.                 this.layer = this.layer.replace(/WOT_POPUP_TEXT_4/g,
  107.                     wot_util.getstring("rating_4") + ":");
  108.             }
  109.  
  110.             if (content.getElementById(this.id)) {
  111.                 return true;
  112.             }
  113.  
  114.             var layer = content.createElement("div");
  115.             layer.setAttribute("id", this.id);
  116.             layer.setAttribute("class", "wot-popup-layer");
  117.             layer.setAttribute("style", "display: none;");
  118.             layer.innerHTML = this.layer;
  119.  
  120.             var style = content.createElement("style");
  121.             style.setAttribute("type", "text/css");
  122.             style.innerHTML = WOT_POPUP_STYLE;
  123.  
  124.             var body = content.getElementsByTagName("body");
  125.             var head = content.getElementsByTagName("head");
  126.  
  127.             if (!body || !body.length || !head || !head.length) {
  128.                 return false;
  129.             }
  130.  
  131.             body[0].appendChild(layer);
  132.             head[0].appendChild(style);
  133.             return true;
  134.         } catch (e) {
  135.             dump("wot_popup.add_popup: failed with " + e + "\n");
  136.         }
  137.         return false;
  138.     },
  139.  
  140.     elem_pos_x: function(elem)
  141.     {
  142.         var curtop = 0;
  143.         try {
  144.             if (elem.offsetParent) {
  145.                 while (elem.offsetParent) {
  146.                     curtop += elem.offsetLeft;
  147.                     elem = elem.offsetParent;
  148.                 }
  149.             } else if (elem.x) {
  150.                 curtop += elem.x;
  151.             }
  152.         } catch (e) {
  153.             dump("wot_popup.elem_pos_x: failed with " + e + "\n");
  154.         }
  155.         return curtop;
  156.     },
  157.  
  158.     elem_pos_y: function(elem)
  159.     {
  160.         var curtop = 0;
  161.         try {
  162.             if (elem.offsetParent) {
  163.                 while (elem.offsetParent) {
  164.                     curtop += elem.offsetTop;
  165.                     elem = elem.offsetParent;
  166.                 }
  167.             } else if (elem.y) {
  168.                 curtop += elem.y;
  169.             }
  170.         } catch (e) {
  171.             dump("wot_popup.elem_pos_x: failed with " + e + "\n");
  172.         }
  173.         return curtop;
  174.     },
  175.  
  176.     loadlayer: function(content, layer, target)
  177.     {
  178.         try {
  179.             var status = wot_cache.get(target, "status");
  180.  
  181.             if (status != WOT_QUERY_OK && status != WOT_QUERY_LINK) {
  182.                 return false;
  183.             }
  184.  
  185.             var cls = layer.getAttribute("class");
  186.  
  187.             if (wot_prefs.accessible) {
  188.  
  189.                 if (!cls || !cls.length) {
  190.                     cls = "accessible";
  191.                 } else if (cls.indexOf("accessible") < 0) {
  192.                     cls += " accessible";
  193.                 }
  194.  
  195.                 layer.setAttribute("class", cls);
  196.             } else if (cls && cls.indexOf("accessible") >= 0) {
  197.                 cls = cls.replace(/accessible/g, "");
  198.                 layer.setAttribute("class", cls);
  199.             }
  200.  
  201.             for (var i = 0; i < WOT_APPLICATIONS; ++i) {
  202.                 var rep = content.getElementById("wot-r" + i + "-rep");
  203.                 var cnf = content.getElementById("wot-r" + i + "-cnf");
  204.  
  205.                 if (!rep || !cnf) {
  206.                     continue;
  207.                 }
  208.  
  209.                 var r = wot_cache.get(target, "reputation_" + i);
  210.                 var c = wot_cache.get(target, "confidence_" + i);
  211.  
  212.                 if (r >= WOT_MIN_REPUTATION_5) {
  213.                     rep.setAttribute("reputation", 5);
  214.                 } else if (r >= WOT_MIN_REPUTATION_4) {
  215.                     rep.setAttribute("reputation", 4);
  216.                 } else if (r >= WOT_MIN_REPUTATION_3) {
  217.                     rep.setAttribute("reputation", 3);
  218.                 } else if (r >= WOT_MIN_REPUTATION_2) {
  219.                     rep.setAttribute("reputation", 2);
  220.                 } else if (r >= 0) {
  221.                     rep.setAttribute("reputation", 1);
  222.                 } else {
  223.                     rep.setAttribute("reputation", 0);
  224.                 }
  225.  
  226.                 if (c >= WOT_MIN_CONFIDENCE_5) {
  227.                     cnf.setAttribute("confidence", 5);
  228.                 } else if (c >= WOT_MIN_CONFIDENCE_4) {
  229.                     cnf.setAttribute("confidence", 4);
  230.                 } else if (c >= WOT_MIN_CONFIDENCE_3) {
  231.                     cnf.setAttribute("confidence", 3);
  232.                 } else if (c >= WOT_MIN_CONFIDENCE_2) {
  233.                     cnf.setAttribute("confidence", 2);
  234.                 } else if (c >= WOT_MIN_CONFIDENCE_1) {
  235.                     cnf.setAttribute("confidence", 1);
  236.                 } else {
  237.                     cnf.setAttribute("confidence", 0);
  238.                 }
  239.             }
  240.  
  241.             wot_popup.offsetheight = 0;
  242.             var bottom = content.getElementById("wot-r0-stack");
  243.  
  244.             if (wot_prefs.show_application_1) {
  245.                 bottom = content.getElementById("wot-r1-stack");
  246.                 bottom.style.display = "block";
  247.             } else {
  248.                 content.getElementById("wot-r1-stack").style.display = "none";
  249.                 wot_popup.offsetheight -= wot_popup.ratingheight;
  250.             }
  251.             if (wot_prefs.show_application_2) {
  252.                 bottom = content.getElementById("wot-r2-stack");
  253.                 bottom.style.display = "block";
  254.             } else {
  255.                 content.getElementById("wot-r2-stack").style.display = "none";
  256.                 wot_popup.offsetheight -= wot_popup.ratingheight;
  257.             }
  258.             if (wot_prefs.show_application_4) {
  259.                 bottom = content.getElementById("wot-r4-stack");
  260.                 bottom.style.display = "block";
  261.             } else {
  262.                 content.getElementById("wot-r4-stack").style.display = "none";
  263.                 wot_popup.offsetheight -= wot_popup.ratingheight;
  264.             }
  265.             bottom.style.borderBottom = "0";
  266.             content.getElementById("wot-ratings").style.height =
  267.                 wot_popup.offsetheight + wot_popup.areaheight + "px";
  268.         
  269.             return true;
  270.         } catch (e) {
  271.             dump("wot_popup.loadlayer: failed with " + e + "\n");
  272.         }
  273.         return false;
  274.     },
  275.  
  276.     hidelayer: function(content)
  277.     {
  278.         try {
  279.             var layer = content.getElementById(this.id);
  280.  
  281.             if (layer && layer.style.display != "none") {
  282.                 layer.style.display = "none";
  283.             }
  284.         } catch (e) {
  285.             dump("wot_popup.hidelayer: failed with " + e + "\n");
  286.         }
  287.     },
  288.  
  289.     findelem: function(event)
  290.     {
  291.         try {
  292.             var elem = event.originalTarget;
  293.             var attr = null;
  294.  
  295.             while (elem) {
  296.                 if (elem.attributes) {
  297.                     attr = elem.attributes.getNamedItem(wot_search.attribute);
  298.                     if (attr && attr.nodeValue) {
  299.                         break;
  300.                     }
  301.                     attr = null;
  302.                 }
  303.                 elem = elem.parentNode;
  304.             }
  305.  
  306.             if (!elem || !attr) {
  307.                 return null;
  308.             }
  309.  
  310.             return elem;
  311.         } catch (e) {
  312.             dump("wot_popup.findelem: failed with " + e + "\n");
  313.         }
  314.         return null;
  315.     },
  316.  
  317.     onmouseover: function(event)
  318.     {
  319.         try {
  320.             if (!wot_prefs.enabled || !wot_prefs.show_search_popup ||
  321.                     !event || !event.view) {
  322.                 return;
  323.             }
  324.  
  325.             var content = event.view.document;
  326.  
  327.             if (!content) {
  328.                 return;
  329.             }
  330.             
  331.             var layer = content.getElementById(wot_popup.id);
  332.  
  333.             if (!layer) {
  334.                 return;
  335.             }
  336.  
  337.             var elem = wot_popup.findelem(event);
  338.  
  339.             if (!elem) {
  340.                 wot_popup.hidelayer(content);
  341.                 return;
  342.             }
  343.  
  344.             var attr = elem.attributes.getNamedItem(wot_search.attribute);
  345.             var target = attr.nodeValue;
  346.  
  347.             if (layer.style.display == "block" &&
  348.                     layer.getAttribute("target") == target) {
  349.                 return;
  350.             }
  351.  
  352.             layer.setAttribute("target", target);
  353.  
  354.             if (!wot_popup.loadlayer(content, layer, target)) {
  355.                 wot_popup.hidelayer(content);
  356.                 return;
  357.             }
  358.  
  359.             var popupheight = wot_popup.height + wot_popup.offsetheight;
  360.             
  361.             layer.style.height = popupheight + "px";
  362.             layer.style.width  = wot_popup.width  + "px";
  363.  
  364.             var height = event.view.innerHeight - wot_popup.barsize;
  365.             var width  = event.view.innerWidth  - wot_popup.barsize;
  366.  
  367.             if (height < popupheight ||    width < wot_popup.width) {
  368.                 wot_popup.hidelayer(content);
  369.                 return
  370.             }
  371.  
  372.             var vscroll = event.view.pageYOffset;
  373.             var hscroll = event.view.pageXOffset;
  374.  
  375.             var y = wot_popup.elem_pos_y(elem);
  376.             var posy = wot_popup.offset + y + elem.offsetHeight;
  377.             var posx = wot_popup.offset + event.pageX;
  378.  
  379.             if (posy + popupheight > height + vscroll) {
  380.                 posy = y - popupheight - wot_popup.offset;
  381.             }
  382.  
  383.             if (posx - hscroll < 0) {
  384.                 posx = hscroll;
  385.             } else if ((posx + wot_popup.width) > (width + hscroll)) {
  386.                 posx = width - wot_popup.width + hscroll;
  387.             }
  388.  
  389.             layer.style.top  = posy + "px";
  390.             layer.style.left = posx + "px";
  391.             layer.style.display = "block";
  392.         } catch (e) {
  393.             dump("wot_popup.onmouseover: failed with " + e + "\n");
  394.         }
  395.     }
  396. };
  397.  
  398. wot_popup.init();
  399.